package br.ucb.ucblive.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.ucb.ucblive.HIB.TopicoHIB;
import br.ucb.ucblive.beans.Topico;


@FacesConverter (value="TopicoConverter")
public class TopicoConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		TopicoHIB topicoHib = new TopicoHIB();
		if (value == null)
			return null;
		try {
			return topicoHib.consultar(Integer.parseInt(value));
		} catch (Exception e) {
			e.printStackTrace();
			return new Object();
		}
	}

	@Override
	public String getAsString(FacesContext context, UIComponent componente, Object object) {
		if (object == null)
			return null;
		Topico topico = (Topico) object;		
		return Integer.toString(topico.getIdTopico());
	}

}
